﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
namespace BIN
{
    static class CheckMSSQLSERVICE
    {
        private static string GetHostName()
        {
            return System.Net.Dns.GetHostName();
        }
        private static string GetIPAddr()
        {
            System.Net.IPAddress[] Addrs =
                System.Net.Dns.GetHostEntry(GetHostName()).AddressList;
            int Len = Addrs.Length;
            for (int i = 0; i < Len; i++)
            {
                if (Addrs[i].IsIPv6LinkLocal ||
                    Addrs[i].IsIPv6Multicast ||
                    Addrs[i].IsIPv6SiteLocal) continue;
                return Addrs[i].ToString();
            }
            return Addrs[0].ToString();
        }
        public static void Check()
        {
            string configFile = ShareMet.CombineDir(
                ShareMet.GetAppDir(), "YSPOS.EXE.config");
            if (!ShareMet.FileExists(configFile)) return;
            string xpath = "//configuration//appSettings//add";
            XmlNodeList nodeList= XPathGet.GetNodeList(configFile, xpath);
            if (null == nodeList || 0 == nodeList.Count) return;
            string ds="";
            foreach (XmlNode node in nodeList)
            {
                if ("SERVERCONNSTR_DATASOURCE".Equals(
                    node.Attributes["key"].InnerText,
                    StringComparison.OrdinalIgnoreCase))
                {
                    ds = node.Attributes["value"].InnerText;
                }
            }
            if ("".Equals(ds)) return;
            bool IsLocalMachine = false;
            if (ds.StartsWith(".") || ds.StartsWith("127.0.0.1") ||
                ds.StartsWith("localhost", StringComparison.OrdinalIgnoreCase))
            {
                IsLocalMachine = true;
            }
            string[] Arr = ds.Split(new char[] {'\\','/' });
            if (null != Arr && 0 < Arr.Length)
            {
                string host = Arr[0];
                if (host.StartsWith(GetHostName(),
                    StringComparison.OrdinalIgnoreCase))
                {
                    IsLocalMachine = true;
                }
                else if (host.StartsWith(GetIPAddr(),
                    StringComparison.OrdinalIgnoreCase))
                {
                    IsLocalMachine = true;
                }
            }
            if (!IsLocalMachine)
            {
                BIN.AutoUpdateLog.LogObj.AddLog(LOG_MODE.INFO, "No check MSSQLSERVER necessary!");
                return;
            }
            string instanceName = "", svcName = "";
            if (2 == Arr.Length)
            {
                instanceName = Arr[1];
                svcName += ("MSSQL$" + instanceName);
            }
            else
            {
                svcName = "MSSQLSERVER";
            }
            AutoUpdateLog.LogObj.AddLog(LOG_MODE.INFO,
                string.Format("Begin Checking local MSSQL Service '{0}' ...", svcName));
            CheckService.CheckSvcRunning(svcName);
        }
    }
}
